package com.fishingwithme.android.ui.screens.my.maintain

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Place
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
import com.fishingwithme.android.R
import com.fishingwithme.android.data.model.SpotApprovalDto
import com.fishingwithme.android.navigation.FishingNavigation
import com.fishingwithme.android.ui.theme.AppDimensions
import com.fishingwithme.android.ui.viewmodels.MyMaintainViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MySpotMaintainScreen(
    navController: NavController,
    viewModel: MyMaintainViewModel = hiltViewModel()
) {
    var disabled by remember { mutableStateOf(false) } // 默认显示已收藏的钓点

    val spots = viewModel.getMaintainingSpots(disabled).collectAsLazyPagingItems()

    Scaffold { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
                .padding(AppDimensions.SPACING_8)
        ) {
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(color = MaterialTheme.colorScheme.background),
                shape = RoundedCornerShape(AppDimensions.ROUNDED_CORNER_12)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(AppDimensions.SPACING_8),
                    horizontalArrangement = Arrangement.End,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    // 切换按钮
                    OutlinedButton(
                        onClick = { disabled = !disabled },
                        modifier = Modifier.height(AppDimensions.BUTTON_HEIGHT_36)
                    ) {
                        Text(
                            text = if (disabled) stringResource(R.string.show_disabled) else stringResource(
                                R.string.show_enabled
                            ),
                            style = MaterialTheme.typography.labelMedium
                        )
                    }
                }
            }

            Spacer(modifier = Modifier.height(AppDimensions.SPACING_16))

            // 钓点列表
            LazyColumn(
                verticalArrangement = Arrangement.spacedBy(AppDimensions.SPACING_8)
            ) {
                items(spots.itemCount) { index ->
                    spots[index]?.let { spot ->
                        SpotItem(spot = spot, navController)
                    }
                }

                // 加载状态
                spots.apply {
                    when {
                        loadState.refresh is LoadState.Loading -> {
                            item {
                                Box(
                                    modifier = Modifier.fillMaxWidth(),
                                    contentAlignment = Alignment.Center
                                ) {
                                    CircularProgressIndicator()
                                }
                            }
                        }

                        loadState.append is LoadState.Loading -> {
                            item {
                                Box(
                                    modifier = Modifier.fillMaxWidth(),
                                    contentAlignment = Alignment.Center
                                ) {
                                    CircularProgressIndicator()
                                }
                            }
                        }

                        loadState.refresh is LoadState.Error -> {

                            item {
                                Box(
                                    modifier = Modifier.fillMaxWidth(),
                                    contentAlignment = Alignment.Center
                                ) {
                                    Text(
                                        text = stringResource(
                                            R.string.error_load_failed
                                        ),
                                        color = MaterialTheme.colorScheme.error
                                    )
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

@Composable
private fun SpotItem(spot: SpotApprovalDto, navController: NavController,) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                val router = "${FishingNavigation.APPROVE_SPOT}?id=${spot.id}&approve=1"
                navController.navigate(router)
            },
        shape = RoundedCornerShape(AppDimensions.ROUNDED_CORNER_12)

    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(AppDimensions.SPACING_16),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 钓点图标
            Icon(
                imageVector = Icons.Default.Place,
                contentDescription = null,
                tint = MaterialTheme.colorScheme.primary,
                modifier = Modifier
                    .size(AppDimensions.ICON_SIZE_32)
                    .clip(RoundedCornerShape(AppDimensions.ROUNDED_CORNER_8))
            )

            Spacer(modifier = Modifier.width(AppDimensions.SPACING_12))

            // 钓点信息
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(
                    text = spot.name,
                    style = MaterialTheme.typography.titleMedium,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )

                Spacer(modifier = Modifier.height(AppDimensions.SPACING_4))

                Text(
                    text = spot.createdAt,
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.outline
                )
            }
        }
    }
}